package com.msung.tbea;

import com.yixing.tech.common.tcp.channel.ChannelContext;
import com.yixing.tech.common.tcp.channel.ChannelInit;
import com.yixing.tech.common.tcp.client.NettyTcpClient;
import com.yixing.tech.common.tcp.handler.MessageHandler;
import com.yixing.tech.common.tcp.pc.TcpConsumer;
import com.yixing.tech.common.tcp.pc.TcpMessage;
import com.yixing.tech.common.tcp.pc.TcpProducer;
import com.yixing.tech.common.tcp.pc.TcpReply;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.concurrent.*;
import java.util.function.Function;

@Slf4j
public class NettyTcpClientDemo {


    public static void testNettyTcpClient() throws Exception {
        MessageHandler messageHandler = new MessageHandler();
        messageHandler.setChannelContext(new ChannelContext());

        BlockingDeque<TcpMessage> deque = new LinkedBlockingDeque<>(100);
        TcpProducer tcpProducer = new TcpProducer(deque);

//        Function<TcpMessage, Void> func = msg -> {
//            log.info("tcpProduceFunc=============");
//            tcpProducer.produce(msg);
//            return null;
//        };
//
//        messageHandler.setProduceFunc(func);
        messageHandler.setProducer(tcpProducer);
        ChannelInit channelInit = new ChannelInit(messageHandler);
        NettyTcpClient nettyTcpClient = new NettyTcpClient(channelInit);

        Function<TcpMessage, TcpReply> consumeFunc = msg -> {
            log.info("consumeMsg====================");
            log.info("分发消息>>>>>>>>>>>>>>TODO");
            return new TcpReply();
        };

        TcpConsumer consumer = new TcpConsumer(deque, consumeFunc);
        Thread thread = new Thread(consumer);
        thread.setName("后台消费者线程");
        thread.start();

        String host = "127.0.0.1";
        int port = 8110;
        nettyTcpClient.connect(host, port);

    }

    public static void main(String... args) throws Exception {
        testNettyTcpClient();
    }
}
